package com.gzz.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gzz.common.R;
import com.gzz.mapper.SceneryMapper;
import com.gzz.pojo.GoodsInfo;
import com.gzz.pojo.Scenery;
import com.gzz.service.OssUploadService;
import com.gzz.service.SceneryService;
import com.gzz.vo.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

import static com.gzz.utils.OssUtils.GoodTypeId;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 周佳成
 * @since 2023-11-20
 */
@RestController
@Slf4j
@RequestMapping("/api/scenery")
public class SceneryController {
	@Autowired
	SceneryService sceneryService;

	@Autowired
	OssUploadService ossUploadService;
	@PostMapping("/upload")
	public R<String> Scupload(String type,@RequestParam("file") MultipartFile multipartFile){
		String imgUrl = ossUploadService.uploadFile(multipartFile,type);
		if(imgUrl!="error"){
			return R.success(imgUrl);
		}
		return R.error(imgUrl);
	}
	@GetMapping("/list")
	R<List<SceneryListVo>> getList(){
		//得学习下mybatis多表查询了
		LambdaQueryWrapper<Scenery> lambdaQueryWrapper = new LambdaQueryWrapper<>();
		lambdaQueryWrapper.orderByDesc(Scenery::getOrderWight);

		List<Scenery> list = sceneryService.list(lambdaQueryWrapper);
		List<SceneryListVo> res = new ArrayList<>();
		for(Scenery scenery : list){
			SceneryListVo sceneryListVo = new SceneryListVo();
			BeanUtils.copyProperties(scenery,sceneryListVo);
			res.add(sceneryListVo);
		}

		return R.success(res);

	}
	@GetMapping("/detail")
	R<SenceryDetailVo> getDetailbyId(Long scId){

		return sceneryService.getDetailbyId(scId);


	}

	@GetMapping("/findlist")
	public R<SceneryList> getSearchList(@RequestParam(name= "pageNum")Integer pageNum, @RequestParam(name = "pageSize")Integer pageSize, @RequestParam(name="searchName")String searchName, @RequestParam(name = "searchAddress")String searchAddress)
	{
		//searchName = '%'+searchName+'%';
		//searchKeyWord = '%' + searchKeyWord + '%';
		QueryWrapper<Scenery> goodsInfoQueryWrapper = new QueryWrapper<>();
		goodsInfoQueryWrapper.like(searchName!="","sc_name",searchName);
		goodsInfoQueryWrapper.like(searchAddress!="","sc_address",searchAddress);
		Page<Scenery> goodsInfoPage = new Page<>(pageNum,pageSize);
		sceneryService.page(goodsInfoPage,goodsInfoQueryWrapper);
		List<Scenery> goodsInfoList = goodsInfoPage.getRecords();
		List<SceneryVo> goodsVoList = sceneryService.getVo(goodsInfoList);
		log.info("结果为："+goodsVoList.toString());
		SceneryList goodsInfoList1 = new SceneryList(goodsVoList,sceneryService.list().size());
		return R.success(goodsInfoList1);
	}

	@GetMapping("/reset")
	public R<SceneryList> getReset(@RequestParam(name="pageNum")Integer pageNum,@RequestParam(name="pageSize")Integer pageSize)
	{
		Page<Scenery> sceneryPage = new Page<>(pageNum,pageSize);
		sceneryService.page(sceneryPage);
		List<Scenery> sceneryList  = sceneryPage.getRecords();
		List<SceneryVo> sceneryVoList = sceneryService.getVo(sceneryList);
		SceneryList sceneryList1 = new SceneryList(sceneryVoList,sceneryService.list().size());
		return R.success(sceneryList1);
	}
	@DeleteMapping("/delete")
	public R GoodsDelete(@RequestParam(name = "scId") Long scId)
	{

		log.info("商品id:"+scId);
		sceneryService.removeByIdAndTypeId(Long.valueOf(scId),GoodTypeId);
		sceneryService.removeById(scId);
		return R.success(true);
	}

	@PutMapping("/update")
	public R goodsUpdate(@RequestBody SceneryVo sceneryVo)
	{
		log.info("结果为"+sceneryVo.toString());
		sceneryService.removeById(sceneryVo.getScId());
		sceneryService.saveVo(sceneryVo);
		return R.success(true);
	}

}

